//
//  FinanceVC.m
//  easyTaxi
//
//  Created by ellenpage on 13-2-21.
//  Copyright (c) 2013年 ellenpage. All rights reserved.
//

#import "Constant.h"
#import "FinanceVC.h"
#import "ZhiFuView.h"
#import "SocketObject.h"
#import "RequestCMD.h"

bool isCheckHuoDe;
bool isCheckXiaoHao;

@interface FinanceVC ()<UIWebViewDelegate>

@property (retain, nonatomic) NSMutableArray *dataArray;

@property (weak, nonatomic)   IBOutlet UITableView *mTBView;
@property (weak, nonatomic)   IBOutlet UILabel *moneyLbl;
@property (strong, nonatomic) IBOutlet UIView *obtainView;
@property (strong, nonatomic) IBOutlet UIView *costView;

@property (retain, nonatomic) ZhiFuView *zhiFuView;

@property (weak, nonatomic) IBOutlet UIView *view1;

@property (weak, nonatomic) IBOutlet UIView *detailView;
@property (weak, nonatomic) IBOutlet UIView *detailView_Small;
@property (weak, nonatomic) IBOutlet UILabel *titleLbl;
@property (weak, nonatomic) IBOutlet UIWebView *financeWebView;
@property (strong, nonatomic) IBOutlet UIView *financeRecordView;

@property (weak, nonatomic) IBOutlet UILabel *phoneNoLbl;
@property (weak, nonatomic) IBOutlet UILabel *daCheDouLbl;


- (IBAction)hideFinanceRecord;

- (IBAction)goBack;

- (IBAction)openChongZhiView;

- (IBAction)openHuoDeList;

- (IBAction)openXiaoHaoList;

- (IBAction)hideDetailView;

- (IBAction)openBuyRecord;

- (IBAction)openCostRecord;

@end

@implementation FinanceVC

#pragma mark - GoBakck - 返回

- (IBAction)hideFinanceRecord
{
    _financeRecordView.hidden = YES;
}

- (IBAction)goBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - System Functions - 系统函数

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self  initialData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [self setMTBView:nil];
    [self setView1:nil];
    [self setMoneyLbl:nil];
    [self setObtainView:nil];
    [self setCostView:nil];
    [self setTitleLbl:nil];
    [self setFinanceWebView:nil];
    [self setFinanceRecordView:nil];
    [self setPhoneNoLbl:nil];
    [self setDaCheDouLbl:nil];
    [super viewDidUnload];
}

- (void)initialData
{
    /*
    NSArray *arrary  = [NSArray arrayWithObjects:@"1",@"2",nil];
    NSArray *arrary1 = [NSArray arrayWithObjects:@"3",@"4",nil];

    _dataArray     = [NSMutableArray arrayWithObjects:arrary,arrary1,nil];

    isCheckHuoDe   = NO;
    isCheckXiaoHao = NO;
        
    [_obtainView.layer setCornerRadius:10];
    [_obtainView.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];//描边颜色
    [_obtainView.layer setBorderWidth:2.0];//描边粗细

    [_costView.layer setCornerRadius:10];
    [_costView.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];//描边颜色
    [_costView.layer setBorderWidth:2.0];//描边粗细
    
    [self dealWithDetailView];
    [self.view addSubview:_detailView];
    _detailView.hidden = YES;
    */
    
    [self.view addSubview:_financeRecordView];
    _financeRecordView.hidden = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getDaCheDou)
                                                 name:kGetDaCheDouNoti
                                               object:nil];//获得打车豆通知
}

- (void)viewWillAppear:(BOOL)animated
{
    _phoneNoLbl.text  = [PassValueClass sharedIntance].teleNumStr;
    _daCheDouLbl.text = [PassValueClass sharedIntance].daCheDouStr;
}

- (void)dealWithDetailView//detailView加入边框
{
    _detailView.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.65f];
    [_detailView_Small.layer setCornerRadius:10];
    [_detailView_Small.layer setBorderColor:[[UIColor whiteColor] CGColor]];//描边颜色
    [_detailView_Small.layer setBorderWidth:2.0];//描边粗细
}

#pragma mark - OpenZhiFuView - 打开支付页面

- (IBAction)openChongZhiView
{
    ZhiFuView *zhiFuView = [[ZhiFuView alloc] init];
    [self.view addSubview:zhiFuView];
}

/*
 
#pragma mark - TableView DataSource - 数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//区间数
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//每个区间的个数
{
    int i = 0;
    if (_dataArray.count > 0)
    {
        NSArray *array = [_dataArray objectAtIndex:section];
        i = array.count + 1;
        if (section == 0)
        {
            if (isCheckHuoDe == NO)
            {
                i = 0;
            }
        }
        else if (section == 1)
        {
            if (isCheckXiaoHao == NO)
            {
                i = 0;
            }
        }
        
    }
    return i;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    switch (section)
    {
        case 0:
        {
            view = _obtainView;
        }
            break;
            
        case 1:
        {
            view = _costView;
        }
            break;

        default:
            return nil;
            break;
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
													reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *adaptV = [[UIView alloc] initWithFrame:CGRectMake(10, 2.5, _mTBView.bounds.size.width - 20, cell.bounds.size.height-2.5)];
    
    UILabel *subjectLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 11, 55, 21)];
    subjectLb.textAlignment = UITextAlignmentCenter;
    [subjectLb setFont:[UIFont systemFontOfSize:13]];
	subjectLb.backgroundColor = [UIColor clearColor];
	[adaptV addSubview:subjectLb];

    UILabel *subjectLb2 = [[UILabel alloc] initWithFrame:CGRectMake(49, 11, 59, 21)];
    subjectLb2.textAlignment = UITextAlignmentCenter;
    [subjectLb2 setFont:[UIFont systemFontOfSize:13]];
	subjectLb2.backgroundColor = [UIColor clearColor];
	[adaptV addSubview:subjectLb2];

    UILabel *subjectLb3 = [[UILabel alloc] initWithFrame:CGRectMake(195, 11, 85, 21)];
    subjectLb3.textAlignment = UITextAlignmentCenter;
    [subjectLb3 setFont:[UIFont systemFontOfSize:13]];
	subjectLb3.backgroundColor = [UIColor clearColor];
	[adaptV addSubview:subjectLb3];

    UIButton *subjectBtn = [[UIButton alloc] initWithFrame:CGRectMake(97, 11, 102, 21)];
    subjectBtn.userInteractionEnabled = NO;
    subjectBtn.titleLabel.textAlignment = UITextAlignmentCenter;
    [subjectBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    
	subjectBtn.backgroundColor = [UIColor clearColor];
	[adaptV addSubview:subjectBtn];
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            adaptV.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.35];

            subjectLb.text       = @"时间";
            subjectLb.textColor  = [UIColor whiteColor];
            
            subjectLb2.text      = @"操作";
            subjectLb2.textColor = [UIColor whiteColor];

            [subjectBtn setTitle:@"支付金额(元)" forState:UIControlStateNormal];
            [subjectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            subjectLb3.text = @"获得打车豆";
            subjectLb3.textColor = [UIColor whiteColor];
        }
        else
        {
            adaptV.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.65];

            subjectLb.text  = @"13-1-1";
            subjectLb2.text = @"注册赠送";
            [subjectBtn setTitle:@"100" forState:UIControlStateNormal];
            [subjectBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
            subjectLb3.text = @"1000";
        }
    }
    
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            adaptV.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.35];

            subjectLb.text  = @"时间";
            subjectLb.textColor = [UIColor whiteColor];

            subjectLb2.text = @"操作";
            subjectLb2.textColor = [UIColor whiteColor];

            [subjectBtn setTitle:@"相关订单" forState:UIControlStateNormal];
            [subjectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            subjectLb3.text = @"消耗打车豆";
            subjectLb3.textColor = [UIColor whiteColor];
        }
        else
        {
            adaptV.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.65];
            
            subjectLb.text  = @"13-2-2";
            subjectLb2.text = @"打车";
            [subjectBtn setTitle:@"点击查看" forState:UIControlStateNormal];
            [subjectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            subjectLb3.text = @"20";
            if (indexPath.row > 0)
            {
                subjectBtn.userInteractionEnabled = YES;
                subjectBtn.tag = indexPath.row - 1;
                [subjectBtn addTarget:self action:@selector(showRecordDetail:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
    
    [cell.contentView addSubview:adaptV];
    return cell;
}


 #pragma mark - TableView Delegate   - 结构
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    int rowInt     = [indexPath row];
    int sectionInt = [indexPath section];
    switch (sectionInt)
    {
        case 1:
        {
        }
            break;
        default:
            break;
    }
}


- (void)showRecordDetail:(UIButton *)sender
{
    //    int i = sender.tag;
    _detailView.hidden = NO;
}

#pragma mark - 打开获得/消耗 列表

- (IBAction)openHuoDeList
{
    if (isCheckHuoDe == NO)
    {
        isCheckHuoDe = YES;
    }
    else
    {
        isCheckHuoDe = NO;
    }
    [_mTBView reloadData];
}

- (IBAction)openXiaoHaoList
{
    if (isCheckXiaoHao == NO)
    {
        isCheckXiaoHao = YES;
    }
    else
    {
        isCheckXiaoHao = NO;
    }
    [_mTBView reloadData];
}
 
 - (IBAction)hideDetailView
 {
 _detailView.hidden = YES;
 }

 */

#pragma mark - OpenRecord - 充值/消费记录

- (IBAction)openBuyRecord
{
    NSString *url_head = [PassValueClass sharedIntance].getAndCostListStr;
    NSString *imsi = [PassValueClass sharedIntance].IMSIStr;
    NSString *url = [NSString stringWithFormat:@"%@?usertype=0&querytypr=0&imsi=%@",url_head,imsi];
    _financeRecordView.hidden = NO;
    _titleLbl.text = @"充值记录";
    [_financeWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];    
}

- (IBAction)openCostRecord
{
    NSString *url_head = [PassValueClass sharedIntance].getAndCostListStr;
    NSString *imsi = [PassValueClass sharedIntance].IMSIStr;
    NSString *url = [NSString stringWithFormat:@"%@?usertype=0&querytypr=1&imsi=%@",url_head,imsi];
    _financeRecordView.hidden = NO;
    _titleLbl.text = @"消耗记录";
    [_financeWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}


#pragma mark - RequestDaCheDou - 查询打车豆

- (void)requestDaCheDou
{
    NSString *string    = [RequestCMD requestCMD:kRequetDaCheDou];
    NSMutableData *data = [RequestCMD getWantedData:string];
    [[SocketObject sharedInstance].gcdSocket writeData:data withTimeout:-1 tag:kRequetDaCheDou];
}

#pragma mark - GetDaCheDou - 获得打车豆

- (void)getDaCheDou
{
    _daCheDouLbl.text = [PassValueClass sharedIntance].daCheDouStr;
}

@end
